package com.starlink.controller.admin;

import com.starlink.common.Constants;
import com.starlink.common.WebResponse;
import com.starlink.common.annotation.WebAccess;
import com.starlink.common.enums.ErrorCode;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.request.manual.AdminLogonParam;
import com.starlink.model.vo.AdminVO;
import com.starlink.service.AdminAccountService;
import com.starlink.utils.CookieUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.Optional;

/**
 * @author zhanghong
 */
@Slf4j
@RestController
@RequestMapping("/admin/account")
@Api(value = "【管理端】-账号模块", tags = {"【管理端】登录/登出/个人信息"})
public class AdminAccountController {

    @Autowired
    private AdminAccountService adminAccountService;

    @ApiOperation(value = "登录", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/login")
    public WebResponse<AdminVO> login(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminLogonParam adminLogonParam) {
        AdminVO admin = adminAccountService.findAdmin(adminLogonParam.getUsername(), adminLogonParam.getPassword());
        admin.setPwd(null);
        request.getSession().setAttribute(Constants.LOGIN_ADMIN, admin);
        response.addCookie(new Cookie(Constants.SESSIONID, CookieUtil.getValue(request, Constants.SESSIONID)));
        return new WebResponse<>(admin);
    }

    @WebAccess(login = true)
    @ApiOperation(value = "登出", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @RequestMapping("/logout")
    public WebResponse logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute(Constants.LOGIN_ADMIN);
        return new WebResponse(true);
    }

    @WebAccess(login = true)
    @ApiOperation(value = "获取个人信息", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/me")
    public WebResponse<AdminVO> me(HttpServletRequest request, HttpServletResponse response) {
        AdminVO admin = (AdminVO)request.getSession().getAttribute(Constants.LOGIN_ADMIN);
        if (admin == null) {
            admin = Optional.ofNullable(request.getParameter("userId"))
                    .filter(item -> !Objects.equals("null", item))
                    .map(userId -> adminAccountService.queryById(Long.parseLong(userId)))
                    .orElse(null);
        }
        AssertUtil.assertAdminNotNull(admin, ErrorCode.NOT_LOGIN);
        return new WebResponse<>(admin);
    }
}
